<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\CoRotation;
use App\Models\CoSchool;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Controllers\AdminController;
use Illuminate\Http\Request;

class CoRotationController extends AdminController
{
    protected $title = '轮播广告管理';
    protected $school_id;
    public function __construct(Request $request)
    {
        $this->school_id = $request->get('school_id',0);
    }
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new CoRotation(), function (Grid $grid) {
            if($this->school_id>0){
                $grid->model()
                    ->where("school_id",$this->school_id)
                    ->paginate(10);
            }
            $grid->id->sortable();
            $grid->school_id->display(function ($id){
                return CoSchool::where('id',$id)->value('name');
            });
            $grid->name;
            $grid->url;
            $grid->status->switch();
            $grid->icon->image('',60,60);
            $grid->column('sort','排序');
            $grid->created_at;
            $grid->updated_at->sortable();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
            $grid->disableViewButton();
            $grid->disableCreateButton();
            $grid->tools(function ($tools) {
                $url = '/admin/coRotation/create?school_id='.$this->school_id;
                $tools->append("<a href=$url class='btn btn-sm btn-success' style='float: right;'><i class='fa fa-save'></i>&nbsp;&nbsp;新增</a>");
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new CoRotation(), function (Show $show) {
            $show->id;
            $show->name;
            $show->url;
            $show->status;
            $show->icon;
            $show->sort;
            $show->created_at;
            $show->updated_at;
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new CoRotation(), function (Form $form) {
            $form->display('id');
            if($this->school_id > 0){
                $CoSchool = new \App\Models\CoSchool();
                $choole=$CoSchool::all()->where('id',$this->school_id)->pluck('name', 'id');
                if($choole){
                    $form->select('school_id')
                        ->options($choole->toArray())->required();
                }
            }else{
                $form->select('school_id','学校名称')
                    ->options(function () {
                        $CoSchool = new \App\Models\CoSchool();
                        $data =  $CoSchool::all()->where('status',1)->pluck('name', 'id');
                        return $data->toArray();
                    })
                    ->required();
            }
            $form->text('name')->required();
            $form->image('icon')->required();
            $form->text('url')->required();
            $form->radio('status')->options(['1'=>'启用','0'=>'禁用'])->default(1);
            $form->text('sort')->default(10);

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
